package com.polaris.live.popup

import android.content.Context
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.core.PositionPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.PopupPushNoticeBinding
import com.polaris.live.im.utils.SessionUtils
import com.polaris.live.resp.bean.PromptNoticeBean
import com.polaris.live.translation.R.string
import com.polaris.live.ui.party.PartyUserStateUtil
import com.polaris.live.utils.image.loadImage
import com.polaris.live.utils.image.transformRounded
import com.polaris.live.utils.route.RouterUtils

/**
 * PushNoticePopup
 *
 * @author Created by lucas on 2023/12/12 12:30
 */
class PushNoticePopup(
    context: Context,
    private val message: PromptNoticeBean,
) : PositionPopupView(context) {

    private lateinit var mBinding: PopupPushNoticeBinding

    override fun getImplLayoutId(): Int {
        return R.layout.popup_push_notice
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupPushNoticeBinding.bind(popupImplView)
        ViewUtils.setMargin(mBinding.root, topMargin = BarUtils.getStatusBarHeight())
        mBinding.tvTime.text =
            StringUtils.getString(string.app_name) + " " + SessionUtils.formatSessionTime(message.timestamp)

        if (message.title.isNullOrEmpty()) {
            mBinding.tvTitle.visibility = GONE
        } else {
            mBinding.tvTitle.text = message.title
        }

        mBinding.tvContent.text = message.content

        if (message.image.isNullOrEmpty()) {
            mBinding.ivImage.visibility = GONE
        } else {
            Glide.with(context)
                .loadImage(message.image)
                .transformRounded(4.dp)
                .into(mBinding.ivImage)
        }

        mBinding.cvBg.setOnSingleClickListener {
            dismiss()
            if (!PartyUserStateUtil.clickUser()) {
                return@setOnSingleClickListener
            }
            if (message.router != null) {
                RouterUtils.routerGoto(message.router)
            }
            if (message.router2 != null) {
                RouterUtils.routerGoto(message.router2)
            }
        }
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }
}